//
//  CJNavigationController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-04-15.
//

import UIKit

open class CJNavigationController: UINavigationController {

    public var cj_navBarBackColor: UIColor?
    public var cj_navBarTintColor: UIColor?
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        view.cj_didSkin = { [weak self] kit in
            self?.cj_skinAction(kit)
        }
    }
    
    // MARK: 全局项目刷新回调(子类继承实现)
    /// 全局项目刷新回调(子类继承实现)
    open func cj_skinAction(_ kit: Any) {
        view.backgroundColor = GV_CJFrame_backColor
        setupNavBarDefaultApperance()
    }
    
    open override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    /// 更新导航栏颜色
    open func setupNavBarDefaultApperance() {
        let backColor = cj_navBarBackColor ?? GV_CJFrame_navBarBackColor
        let tintColor = cj_navBarTintColor ?? GV_CJFrame_navBarTintColor
        setupNavBarApperance(backColor: backColor, tintColor: tintColor)
    }
    
    /// 更新导航栏颜色
    open func setupNavBarApperance(backColor: UIColor = .white,
                              tintColor: UIColor = .black) {
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundColor = backColor
            appearance.shadowColor = UIColor.clear
            appearance.titleTextAttributes = [.foregroundColor: tintColor]
            navigationBar.standardAppearance = appearance
            navigationBar.scrollEdgeAppearance = appearance
        } else {
            navigationBar.isTranslucent = false
            navigationBar.barTintColor = backColor
            navigationBar.tintColor = tintColor
            navigationBar.titleTextAttributes = [.foregroundColor: tintColor]
            
            navigationBar.setBackgroundImage(UIImage(), for: .default)
            navigationBar.shadowImage = UIImage()
        }
    }
    
}
